//
//  FindMyPosition.swift
//  Swift-30-demo
//
//  Created by apple on 2021/11/23.
//

import UIKit
import CoreLocation

class FindMyPosition: UIViewController, CLLocationManagerDelegate {
    let locationManager = CLLocationManager()
    let geocoder = CLGeocoder()
    let locationLabel = UILabel()
    let locationStrLabel = UILabel()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "FindMyPosition"
        self.view.backgroundColor = UIColor.white
        // Do any additional setup after loading the view.
        
        let bgImageView = UIImageView()
        bgImageView.image = UIImage(named: "image_bg_1")
        self.view.addSubview(bgImageView)
        bgImageView.snp.makeConstraints { make in
            make.top.equalTo(navgationHeight)
            make.leading.equalTo(self.view)
            make.trailing.equalTo(self.view)
            make.bottom.equalTo(self.view)
        }
        
        let blurEffect: UIBlurEffect = UIBlurEffect(style: .light)
        let blurView: UIVisualEffectView = UIVisualEffectView(effect: blurEffect)
        blurView.frame = self.view.bounds
        blurView.alpha = 0.8;
        bgImageView.addSubview(blurView)
        
        locationManager.delegate = self
        
        locationLabel.textAlignment = .center
        locationLabel.textColor = UIColor.white
        self.view.addSubview(locationLabel)
        locationLabel.snp.makeConstraints { make in
            make.top.equalTo(50 + navgationHeight)
            make.leading.equalTo(self.view).offset(20)
            make.trailing.equalTo(self.view).offset(-20)
            make.height.equalTo(100)
        }
        
        locationStrLabel.textAlignment = .center
        locationStrLabel.textColor = UIColor.white
        locationStrLabel.numberOfLines = 0
        self.view.addSubview(locationStrLabel)
        locationStrLabel.snp.makeConstraints { make in
            make.top.equalTo(locationLabel.snp.bottom)
            make.leading.equalTo(self.view).offset(20)
            make.trailing.equalTo(self.view).offset(-20)
            make.height.equalTo(100)
        }
        
        let findMyLocationBtn = UIButton(type: .custom)
        findMyLocationBtn.setTitle("Find My Position", for: .normal)
        findMyLocationBtn.setTitleColor(UIColor.white, for: .normal)
        findMyLocationBtn.addTarget(self, action: #selector(findMyLocation), for: .touchUpInside)
        self.view.addSubview(findMyLocationBtn)
        findMyLocationBtn.snp.makeConstraints { make in
            make.bottom.equalTo(-tabBarHeight - 80)
            make.leading.equalTo(self.view).offset(50)
            make.trailing.equalTo(self.view).offset(-50)
            make.height.equalTo(50)
        }
    }
    
    @objc func findMyLocation() {
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let locations : NSArray = locations as NSArray
        let currentLocation = locations.lastObject as! CLLocation
        let locationStr = "lat:\(currentLocation.coordinate.latitude) lng:\(currentLocation.coordinate.longitude)"
        locationLabel.text = locationStr
        reverseGeocode(location:currentLocation)
        locationManager.stopUpdatingLocation()
    }
    
    ///将经纬度转换为城市名
    func reverseGeocode(location:CLLocation) {
        geocoder.reverseGeocodeLocation(location) { (placeMark, error) in
            if(error == nil) {
                let tempArray = placeMark! as NSArray
                let placemark = tempArray.firstObject as? CLPlacemark
                // now begins the reverse geocode
                let address = "\(placemark?.thoroughfare ?? ""), \(placemark?.locality ?? ""), \(placemark?.subLocality ?? ""), \(placemark?.administrativeArea ?? ""), \(placemark?.postalCode ?? ""), \(placemark?.country ?? "")"
                
                self.locationStrLabel.text = "\(address)"
            }
        }
    }
}
